package com.yao.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yao.mall.common.ErrorCode;
import com.yao.mall.config.BusinessException;
import com.yao.mall.mapper.*;
import com.yao.mall.model.dto.AttrKeyAddRequest;
import com.yao.mall.model.dto.AttrKeySearchRequest;
import com.yao.mall.model.dto.AttrKeyUpdateRequest;
import com.yao.mall.model.entity.*;
import com.yao.mall.model.vo.AttrKeyVO;
import com.yao.mall.model.vo.BrandVO;
import com.yao.mall.service.AttrKeyService;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author jiahua.yao
 * @description 针对表【attrKey】的数据库操作Service实现
 * @date 2024-03-10 20:41:17
 */
@Service
public class AttrKeyServiceImpl extends ServiceImpl<AttrKeyMapper, AttrKey>
        implements AttrKeyService {
    @Resource
    private AttrKeyMapper attrKeyMapper;

    @Resource
    private CategoryMapper categoryMapper;

    @Resource
    private GoodsMapper goodsMapper;

    @Resource
    private SkuAttrValueMapper skuAttrValueMapper;

    @Resource
    private SpuAttrValueMapper spuAttrValueMapper;

    @Override
    public List<AttrKeyVO> getAttrKey() {
        List<AttrKey> attrKeyList = attrKeyMapper.selectList(null);
        return attrKeyList.stream().map(attrKey -> {
            AttrKeyVO attrKeyVO = new AttrKeyVO();
            BeanUtils.copyProperties(attrKey, attrKeyVO);
            return attrKeyVO;
        }).toList();
    }

    @Override
    public List<AttrKeyVO> toAttrKeyVO(List<AttrKey> attrKeyList) {
        return attrKeyList.stream().map(attrKey -> {
            AttrKeyVO attrKeyVO = new AttrKeyVO();
            BeanUtils.copyProperties(attrKey, attrKeyVO);
            Long categoryId = attrKey.getCategoryId();
            Category category = categoryMapper.selectById(categoryId);
            attrKeyVO.setCategory(category);
            attrKeyVO.setKeyId(attrKey.getId());
            return attrKeyVO;
        }).toList();
    }

    @Override
    public Page<AttrKeyVO> getAttrKeyByConditions(AttrKeySearchRequest attrKeySearchRequest) {
        long pageSize = attrKeySearchRequest.getPageSize();
        long current = attrKeySearchRequest.getCurrent();
        QueryWrapper<AttrKey> attrKeyQueryWrapper = new QueryWrapper<>();
        Long categoryId = attrKeySearchRequest.getCategoryId();
        if (categoryId != null) {
            attrKeyQueryWrapper.like("category_id", categoryId);
        }
        String keyName = attrKeySearchRequest.getKeyName();
        if (StringUtils.isNotBlank(keyName)) {
            attrKeyQueryWrapper.like("key_name", keyName);
        }
        PageDTO<AttrKey> userRecords = this.page(new PageDTO<>(current, pageSize), attrKeyQueryWrapper);
        List<AttrKey> records = userRecords.getRecords();
        List<AttrKeyVO> attrKeyVOList = toAttrKeyVO(records);
        PageDTO<AttrKeyVO> attrKeyVOPageDTO = new PageDTO<>(userRecords.getCurrent(), userRecords.getSize(), userRecords.getTotal());
        attrKeyVOPageDTO.setRecords(attrKeyVOList);
        return attrKeyVOPageDTO;
    }

    @Override
    public Boolean addAttrKey(AttrKeyAddRequest attrKeyAddRequest) {
        Long categoryId = attrKeyAddRequest.getCategoryId();
        String keyName = attrKeyAddRequest.getKeyName();
        Long attrType = attrKeyAddRequest.getAttrType();
        if (StringUtils.isBlank(keyName) || categoryId == null || attrType == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "必填字段不能为空！");
        }
        AttrKey attrKey = new AttrKey();
        BeanUtils.copyProperties(attrKeyAddRequest, attrKey);
        return this.save(attrKey);
    }

    @Override
    public Boolean deleteAttrKey(Long id) {
        //注意如果有属性值关联该品牌，则该品牌不可以删除
        QueryWrapper<SkuAttrValue> skuAttrValueQueryWrapper = new QueryWrapper<>();
        skuAttrValueQueryWrapper.eq("key_id", id);
        if (skuAttrValueMapper.selectCount(skuAttrValueQueryWrapper) > 0) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR, "有关联的属性值存在，不可删除！");
        }
        QueryWrapper<SpuAttrValue> spuAttrValueQueryWrapper = new QueryWrapper<>();
        spuAttrValueQueryWrapper.eq("key_id", id);
        if (spuAttrValueMapper.selectCount(spuAttrValueQueryWrapper) > 0) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR, "有关联的属性值存在，不可删除！");
        }
        int i = attrKeyMapper.deleteById(id);
        return i > 0;
    }

    @Override
    public Boolean updateAttrKey(AttrKeyUpdateRequest attrKeyUpdateRequest) {
        if (attrKeyUpdateRequest.getId() == null || StringUtils.isBlank(attrKeyUpdateRequest.getKeyName())||attrKeyUpdateRequest.getCategoryId() == null) {
            throw new BusinessException(ErrorCode.NULL_ERROR, "必要参数为空");
        }
        AttrKey attrKey = new AttrKey();
        BeanUtils.copyProperties(attrKeyUpdateRequest,attrKey);
        return attrKeyMapper.updateById(attrKey) > 0;
    }
}




